package cinema.service;

import cinema.model.*;
import cinema.util.*;
import java.time.*;
import java.util.*;

public class ShowtimeService {
    private static final List<Showtime> showtimes = new ArrayList<>();
    private static final List<Theater> theaters = List.of(
            new Theater("黄金厅", 8, 10),
            new Theater("白银厅", 10, 12),
            new Theater("翡翠厅", 7, 8),
            new Theater("钻石厅", 7, 8),
            new Theater("宝石厅", 8, 9)
    );

    public static void manageShowtimes(User manager) {
        while (true) {
            System.out.println("\n===排片管理===");
            System.out.println("1. 添加 2. 修改 3. 删除 4. 列表 5. 返回");
            switch (InputUtil.readInt("请选择: ")) {
                case 1 -> {
                    Movie m = MovieService.getByTitle(InputUtil.readLine("片名: "));
                    if (m==null) { System.out.println("影片不存在"); break; }
                    Theater t = theaters.get(InputUtil.readInt("厅号(0-4): "));
                    LocalDateTime time = LocalDateTime.parse(InputUtil.readLine("时间(yyyy-MM-ddTHH:mm): "));
                    double price = InputUtil.readDouble("价格: ");
                    showtimes.add(new Showtime(m, t, time, price));
                    Logger.log("添加场次", m.getTitle());
                }
                case 2 -> System.out.println("修改略");
                case 3 -> {
                    int idx = InputUtil.readInt("删除场次序号: ");
                    if (idx>=0 && idx<showtimes.size()) {
                        Showtime s = showtimes.get(idx);
                        if (TicketService.hasSold(s)) { System.out.println("已售票，不能删"); break; }
                        showtimes.remove(idx);
                        Logger.log("删除场次", s.getMovie().getTitle());
                    }
                }
                case 4 -> listShowtimes();
                case 5 -> { return; }
            }
        }
    }

    public static void listShowtimes() {
        for (int i=0;i<showtimes.size();i++) {
            Showtime s = showtimes.get(i);
            System.out.printf("%d. %s %s %s %.2f元\n", i, s.getMovie().getTitle(), s.getTheater().getName(), s.getTime(), s.getPrice());
        }
    }

    public static boolean isScheduled(Movie m) {
        return showtimes.stream().anyMatch(s -> s.getMovie().equals(m));
    }

    public static List<Showtime> getAll() { return showtimes; }
}